package com.xing.poi_dome.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xing.poi_dome.entity.PoiEntity;
import com.xing.poi_dome.service.PoiService;
import com.xing.poi_dome.utils.PoiUtils;
import com.xing.poi_dome.utils.Result;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/7/8 14:15
 */
@Tag(name = "文件生成")
@Slf4j
@RequestMapping("/file")
@RestController
@CrossOrigin
public class FileController {

    @Autowired
    private PoiService service;
    @Value("${file.path}")
    private String path;

    @Operation(summary = "模板上传")
    @PostMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        String name = path + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".docx";
        System.out.println(name);
        String filePath = "";
        try {
            File file1 = new File(name);
            if (!file1.exists()) {
                file1.createNewFile();
            }
            file.transferTo(file1);
            filePath = file1.getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().err(501);
        }
        return new Result().ok(filePath);
    }

    @Operation(summary = "新建模板")
    @PostMapping("save")
    public Result save(@RequestBody PoiEntity poiEntity) {
        log.info("poiEntity:{}", poiEntity);
        service.save(poiEntity);
        return new Result().ok();
    }


    @Operation(summary = "修改模板数据")
    @PostMapping("settings")
    public Result settings(@RequestBody PoiEntity entity) {
        log.info("entity:{}", entity);
        PoiEntity byId = service.getById(entity.getId());
        byId.setData(entity.getData());
        service.updateById(byId);
        return new Result().ok();
    }

    @Operation(summary = "删除模板")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id) {
        service.removeById(id);
        return new Result().ok();
    }

    @Operation(summary = "获取模板列表")
    @GetMapping("page")
    public Result page(@RequestParam Map<String, Object> params) {
        log.info("params:{}", params);
        IPage<PoiEntity> page = service.Page(params);
        return new Result().ok(page);
    }

    @Operation(summary = "导出文件")
    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        PoiEntity byId = service.getById((Serializable) params.get("id"));
        PoiUtils.template(byId.getPath(), byId.getData(), response);
    }


}
